package com.itrencai.d3_polymorphic.api_stringbuilder;

//打印整型数组内容
public class StringBuilderDemo2 {
    //设计一个方法用于输出任意整型数组内容
    public static void main(String[] args) {
        int[] arr1=null;
        System.out.println(toString(arr1));
        int[] arr2={1,2,3,4,5};
        System.out.println(toString(arr2));


    }
    public static  String  toString(int[] arr){
        if(arr!=null){
            //开始拼接内容
            StringBuilder sb=new StringBuilder("[");
            for (int i = 0; i < arr.length; i++) {
                sb.append(arr[i]).append(i==arr.length-1?"":", ");
            }
            sb.append("]");
            return sb.toString();
        }else{
            return null;
        }
    }
}
